<?php
	class News extends MY_controller{

		function __construct(){
			parent::__construct();
		}

		function index($title = 0, $school_id = 0, $limit = 0){
			if($this->input->post()){
				$limit = 0;
				$post_data = $this->input->post();
				$title = $post_data['title'] ? urldecode($post_data['title']) : 0;
			}else{
				$title = urldecode($title);
			}
			$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
			$conditions = array('title' => $title, 'school_id' => $school_id);
			$this->load->model("News_model");
			$result = $this->News_model->get_news_list($limit, $this->offset, $conditions);
			$this->load->library('pagination');
			$page_config['base_url'] = '/news/index/' . $title . '/' . $school_id;
			$page_config['total_rows'] = $result['count'];
			$page_config['per_page'] = $this->offset;
			$page_config['uri_segment'] = 5;
			$this->pagination->initialize($page_config);
			$pagination = $this->pagination->create_links();
			$this->smarty->assign("action", '资讯列表');
			$this->smarty->assign("title", $title);
			$this->smarty->assign("current_url", $current_url);
			$this->smarty->assign("item_list", $result['result']);
			$this->smarty->assign("pagination", $pagination);
			$this->smarty->assign("type", 9);
            $school_info['id'] = $school_id;
            $this->smarty->assign("school_info", $school_info);
			$this->smarty->display("news/list.html");
		}

		function add(){
			$post_data = $this->input->post();
			if($post_data){
				$post_data['add_time'] = time();
				if(!empty($post_data['news_images'])){
					$post_data['news_images'] = get_qiniu_url($post_data['news_images']);
				}
				if(!empty($post_data['pics'])){
					foreach ($post_data['pics'] as $key => $value) {
						$post_data['pics'][$key] = get_qiniu_url($value);
					}
					$post_data['pics'] = json_encode($post_data['pics']);
				}
				if(isset($post_data['file'])) unset($post_data['file']);
				$this->db->insert("news", $post_data);
				$insert_id = $this->db->insert_id();
				if($insert_id > 0){
					show_message($_SERVER['HTTP_REFERER']);
				}else{
					show_message($_SERVER['HTTP_REFERER'], "操作失败");
				}
			}else{
				$this->load->model("News_categorys");
				$top_category = $this->News_categorys->get_top_categorys();
				$this->smarty->assign("action", '添加资讯/帮助');
				$this->smarty->assign("top_category", $top_category);
				$this->smarty->assign("post_url", '/news/add');
				$this->smarty->display("news/save.html");
			}
		}

		function edit($id = 0){
			$post_data = $this->input->post();
			if($post_data){
				if(!empty($post_data['news_images'])){
					$post_data['news_images'] = get_qiniu_url($post_data['news_images']);
				}
				if($post_data['top_category_id'] == ''){
					unset($post_data['top_category_id']);
				}
				if(isset($post_data['file'])) unset($post_data['file']);
				$this->db->where('id', $post_data['id']);
				$this->db->update('news', $post_data);
				if($this->db->affected_rows() > 0){
					show_message($_SERVER['HTTP_REFERER']);
				}else{
					show_message($_SERVER['HTTP_REFERER'], "操作失败");
				}
			}else{
				if(intval($id) > 0){
					$news_info = get_field_by_where("news", "*", "id='{$id}'");
					if(!$news_info){
						show_message("/news/type_list", "数据不存在");exit;
					}
					$this->load->model("News_categorys");
					$top_category = $this->News_categorys->get_top_categorys();
					if($news_info['category_id'] > 0){
						$category = $this->News_categorys->get_children_category($news_info['top_category_id']);
						$this->smarty->assign("category", $category);
					}
					$this->smarty->assign("top_category", $top_category);
					$this->smarty->assign("news_info", $news_info);
					$this->smarty->assign("action", '编辑专题');
					$this->smarty->assign("post_url", '/news/edit');
					$this->smarty->display("news/save.html");
				}else{
					show_message("/news", "非法操作");
				}
			}
		}

		function delete($ids = 0){
			if($_POST){
				$ids = $this->input->post('ids');
			}else{
				$ids = (array) $ids;
			}
			delete_data($ids, "news");
			if($this->db->affected_rows() > 0){
				show_message($_SERVER['HTTP_REFERER'], "删除成功");
			}else{
				show_message($_SERVER['HTTP_REFERER'], "删除失败，请重试！");
			}
		}

		function ignore_get_children_category(){
			if(is_ajax_request()){
				$post_data = $this->input->post();
				if(!empty($post_data['category_id'])){
					$this->load->model("News_categorys");
					$category_list = $this->News_categorys->get_children_category($post_data['category_id']);
					// print_r($category_list);
					if($category_list){
						echo json_encode(array("code" => 1, "category_list" => $category_list));
					}else{
						echo json_encode(array("code" => 0));
					}
				}else{
					echo json_encode(array("code" => 0));
				}
			}
		}
	}
